#include "mainpage.h"
#include "ui_mainpage.h"
#include "netmain.h"
#include <QDebug>
#include <QTimer>

MainPage::MainPage(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::MainPage)
{
    ui->setupUi(this);

    connect(carMoveTimer, &QTimer::timeout,
            this, &MainPage::carMoveTimeTimeoutSlot);
    roundList.append(0);
    roundList.append(6);
    roundList.append(10);
    roundList.append(5);
    roundList.append(13);
    roundList.append(9);


    connect(NetMain::getObject(), &NetMain::setCarAlarmSig,
            this, &MainPage::setCarAlarmSlot);
    connect(NetMain::getObject(), &NetMain::setCarAlarmLightSig,
            this, &MainPage::setCarAlarmLightSlot);
    connect(NetMain::getObject(), &NetMain::setCarLightSig,
            this, &MainPage::setCarLightSlot);
    connect(NetMain::getObject(), &NetMain::setCarStatusSig,
            this, &MainPage::setCarStatusSlot);
}

MainPage::~MainPage()
{
    delete ui;
}

void MainPage::carMoveTimeTimeoutSlot()
{
    roundPos++;
    if(roundPos > 9)
    {
        roundIndex++;
        roundPos =0;
        if(roundIndex >= roundList.length())
            roundIndex = 0;
    }
    NetMain::getObject()->carPosChanged(roundList.at(roundIndex), roundPos);
}

void MainPage::setCarAlarmSlot(bool sw)
{
    qDebug()<<"alarm"<< sw;
}

void MainPage::setCarAlarmLightSlot(bool sw)
{

}

void MainPage::setCarLightSlot(bool sw)
{

}

void MainPage::setCarStatusSlot(bool sw)
{
    if(sw)
        carMoveTimer->start(100);
    else
        carMoveTimer->stop();
}
